import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/page/auto_makers/shop_home/shop_comment.dart';
import 'package:ytbusiness/page/drawer/draw_layout.dart';
import 'package:ytbusiness/utils/helper.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';

class RepairFactoryHome extends StatefulWidget {
  @override
  _RepairFactoryHomeState createState() => _RepairFactoryHomeState();
}

class _RepairFactoryHomeState extends State<RepairFactoryHome> {
  List<String> imageList = [
    "maintain.png",
    "procurement.png",
    "mountings.png",
    "account.png"
  ];
  List<String> titleList = ["推修订单", "采购订单", "配件采购", "账户管理"];
  DateTime _lastPressedAt; //上次点击时间
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (_lastPressedAt == null ||
            DateTime.now().difference(_lastPressedAt) > Duration(seconds: 1)) {
          //两次点击间隔超过1秒则重新计时
          _lastPressedAt = DateTime.now();
          return false;
        }
        return true;
      },
      child: Scaffold(
        backgroundColor: Color(0xffF0F0F0),
        appBar: AppBar(
          elevation: 0,
          centerTitle: true,
          backgroundColor: YTTheme.theme,
          title: Text(
            "云途养护",
            style: TextStyle(color: Colors.white, fontSize: setSp(19)),
          ),
          leading: Builder(
            builder: (BuildContext context) {
              return GestureDetector(
                onTap: () {
                  Scaffold.of(context).openDrawer();
                },
                child: Row(
                  children: [
                    Container(
                      margin: EdgeInsets.only(left: setWidth(15)),
                      width: setWidth(33),
                      height: setWidth(33),
                      decoration: BoxDecoration(
                          image: DecorationImage(
                            image: AssetImage("assets/images/touxiang.png"),
                            fit: BoxFit.cover,
                          ),
                          borderRadius: BorderRadius.circular(25)),
                    ),
                  ],
                ),
              );
            },
          ),
          actions: [
            Row(
              children: [
                GestureDetector(
                  onTap: () {
                    urlPush(context, "yuntu://message_center");
                  },
                  child: Container(
                    width: setWidth(22),
                    height: setWidth(21),
                    child: Image.asset("assets/images/message.png"),
                  ),
                ),
                SizedBox(
                  width: setWidth(20),
                )
              ],
            )
          ],
        ),
        //抽屉
        drawer: DrawerLayout(),
        body: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              SizedBox(
                height: setWidth(15),
              ),
              Container(
                padding: EdgeInsets.only(
                    top: setWidth(17),
                    bottom: setWidth(17),
                    left: setWidth(15),
                    right: setWidth(15)),
                color: Colors.white,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Row(
                      children: [
                        Container(
                          height: setWidth(50),
                          width: setWidth(60),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(3),
//                              image: DecorationImage(
//                                  image: NetworkImage("https://i.loli.net/2020/11/25/e8H2umapcJO7Zyo.png"),fit: BoxFit.cover
//                              )
                          ),
                          child: Image.asset("assets/images/car.png",
                              fit: BoxFit.cover),
                        ),
                        SizedBox(
                          width: setWidth(10),
                        ),
                        Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              "云达汽贸",
                              style: TextStyle(
                                  color: Color(0xff323232),
                                  fontSize: setSp(16)),
                            ),
                            SizedBox(
                              height: setWidth(12),
                            ),
                            Text(
                              "宝马奥迪本田汽配中心",
                              style: TextStyle(
                                  color: Color(0xff999999),
                                  fontSize: setSp(12)),
                            ),
                          ],
                        ),
                      ],
                    ),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Container(
                          height: setWidth(13),
                          width: setWidth(11),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(3),
                              image: DecorationImage(
                                  image:
                                      AssetImage("assets/images/location.png"),
                                  fit: BoxFit.cover)),
                        ),
                        SizedBox(
                          width: setWidth(10),
                        ),
                        Text(
                          "安徽 合肥",
                          style: TextStyle(
                              color: YTTheme.theme, fontSize: setSp(12)),
                        ),
                      ],
                    )
                  ],
                ),
              ),
              SizedBox(
                height: setWidth(15),
              ),
              _textBanner(),
              Container(
                padding: EdgeInsets.only(
                    top: setWidth(20),
                    bottom: setWidth(20),
                    left: setWidth(15),
                    right: setWidth(15)),
                color: Colors.white,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    _orderItem(price: "100", title: "新推修订单"),
                    _orderItem(price: "100", title: "维修中的订单"),
                    _orderItem(price: "100", title: "账户余额")
                  ],
                ),
              ),
              SizedBox(
                height: setWidth(15),
              ),
              Container(
                padding:
                    EdgeInsets.only(top: setWidth(17), bottom: setWidth(19)),
                color: Colors.white,
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: titleList
                        .asMap()
                        .map((key, value) {
                          return MapEntry(
                              key,
                              _navItem(
                                  title: titleList[key],
                                  image: imageList[key],
                                  index: key));
                        })
                        .values
                        .toList()),
              ),
              SizedBox(
                height: setWidth(15),
              ),
              //店铺评论
              Container(
                color: Colors.white,
                padding:
                    EdgeInsets.only(top: setWidth(15), bottom: setWidth(13)),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                      child: Text(
                        "店铺评论",
                        style: TextStyle(
                            color: Color(0xff323232),
                            fontSize: setSp(17),
                            fontWeight: FontWeight.w400),
                      ),
                    ),
                  ],
                ),
              ),
              //店铺评论
              ShopComment(),
            ],
          ),
        ),
      ),
    );
  }

  //文字走马灯
  Widget _textBanner() {
    return Container(
      padding: EdgeInsets.only(
          top: setWidth(10),
          left: setWidth(10),
          right: setWidth(10),
          bottom: setWidth(10)),
      decoration: BoxDecoration(
        color: Colors.white,
      ),
      height: setWidth(40),
      child: Swiper(
        autoplay: true,
        scrollDirection: Axis.vertical,
        physics: NeverScrollableScrollPhysics(),
        duration: 1000,
        itemBuilder: (BuildContext context, int index) {
          return RichText(
              text: TextSpan(children: [
            WidgetSpan(
              child: Container(
                child: Image.asset(
                  "assets/images/notice.png",
                  width: setWidth(25),
                  height: setWidth(16),
                  fit: BoxFit.cover,
                ),
                margin: EdgeInsets.only(right: setWidth(8)),
              ),
            ),
            TextSpan(
                text: "促销活动报名进行中...",
                style:
                    TextStyle(color: Color(0xff646464), fontSize: setSp(14))),
          ]));
        },
        itemCount: 3,
      ),
    );
  }

  //导航
  Widget _navItem({String title, String image, int index}) {
    return Expanded(
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          switch (index) {
            case 0:
              urlPush(context, "yuntu://push_repair");
              break;
            case 1:
              urlPush(context, "yuntu://procurement_all_order");
              break;
            case 2:
              urlPush(context, "yuntu://accessories_query");
              break;
            case 3:
              urlPush(context, "yuntu://repair_account_management");
              break;
          }
        },
        child: Column(
          children: [
            Container(
              height: setWidth(44),
              width: setWidth(44),
              child: Image.asset(
                "assets/images/$image",
                fit: BoxFit.cover,
              ),
            ),
            SizedBox(
              height: setWidth(10),
            ),
            Container(
              child: Text(
                title,
                style: TextStyle(
                    color: Color(0xff323232),
                    fontSize: setSp(12),
                    fontWeight: FontWeight.w400),
              ),
            ),
          ],
        ),
      ),
      flex: 1,
    );
  }

  //订单
  Widget _orderItem({String price, String title}) {
    return Expanded(
      child: Container(
        child: Column(
          children: [
            Container(
              child: Text(
                price,
                style: TextStyle(
                    color: Color(0xff333333),
                    fontSize: setSp(23),
                    fontWeight: FontWeight.w600),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            SizedBox(
              height: setWidth(10),
            ),
            Container(
              child: Text(
                title,
                style: TextStyle(
                    color: Color(0xff989898),
                    fontSize: setSp(12),
                    fontWeight: FontWeight.w600),
              ),
            ),
          ],
        ),
      ),
      flex: 1,
    );
  }
}
